package com.gimi.cloud.mmf.gateway.common.reqhead;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.gimi.cloud.mmf.common.utils.JsonUtil;
import com.gimi.cloud.mmf.gateway.common.enums.ResponseCodeEnum;
import org.apache.commons.io.IOUtils;



import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Util {

    public static String createRequstJson(RequestHeader header, Object body) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("head", header);
        map.put("body", body);
        String jsonStr = JsonUtil.objectToJson(map);

        return jsonStr;
    }

    public static String createRequstJson(String header, String body) {
        StringBuffer sb = new StringBuffer();
        sb.append("{");
        sb.append("\"head\":");
        sb.append(header);
        sb.append(",");
        sb.append("\"body\":");
        sb.append(body);
        sb.append("}");
        return sb.toString();
    }

    public static String createResponseJson(Response header, Object body) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("head", header);
        map.put("body", JSONObject.fromObject(body));
        String jsonStr = JsonUtil.objectToJson(map);

        return jsonStr;
    }

    public static String createResponseJsonArray(Response header, Object body) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("head", header);
		map.put("body", JSONArray.fromObject(body));
		String jsonStr = JsonUtil.objectToJson(map);
		return jsonStr;
	}
    
    /**
     * 返回响应
     *
     * @return
     * @throws Exception
     */
    public static void sendResponse(String responseString, HttpServletResponse response) {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter pw = null;
        try {
            response.setContentLength(responseString.getBytes("utf-8").length);
            pw = response.getWriter();
            pw.write(responseString);
            pw.flush();
        } catch (UnsupportedEncodingException ue) {
            ue.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(pw);
        }
    }

    /**
     * 返回响应
     *
     * @return
     * @throws Exception
     */
    public static void sendResponseHtml(String responseString, HttpServletResponse response) {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = null;
        try {
            response.setContentLength(responseString.getBytes("utf-8").length);
            pw = response.getWriter();
            pw.write(responseString);
            pw.flush();
        } catch (UnsupportedEncodingException ue) {
            ue.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(pw);
        }
    }

    /**
     * 返回响应
     *
     * @return
     * @throws Exception
     */
    public static void sendResponse(Object data, ResponseCodeEnum codeEnum, HttpServletResponse response) {
    	Response resp = new Response();
		resp.setRes_data(data);
		resp.setResponseCode(codeEnum);
		Util.sendResponse(JsonUtil.objectToJsonDateSerializerFormatYYYYMMDDHHMMSS(resp), response);
    }

}
